﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Data.OleDb;
using System.Data.SqlClient;
using PersonnelStatistics.DAL;

namespace 人员统计
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string url = textBox1.Text;
            string name = textBox2.Text;
            string pwd = textBox3.Text;
            string sql = textBox4.Text;
            string title = textBox6.Text;
            string path4 = Directory.GetCurrentDirectory() + "\\set.ini";
            //清空配置文件
            try
            {
               //如果set.ini不存在
                FileStream fs1 = new FileStream(path4, FileMode.Truncate, FileAccess.ReadWrite);
                fs1.Close();
                //写入新配置文件
                FileStream fs = new FileStream(path4, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(url);
                sw.WriteLine(name);
                sw.WriteLine(pwd);
                sw.WriteLine(sql);
                sw.WriteLine(title);
                sw.Close();
                fs.Close();
                MessageBox.Show("配置保存成功！");
            }
            catch
            {
                //创建新文件写入
                //写入新配置文件
                FileStream fs = new FileStream(path4, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(url);
                sw.WriteLine(name);
                sw.WriteLine(pwd);
                sw.WriteLine(sql);
                sw.WriteLine(title);
                sw.Close();
                fs.Close();
                MessageBox.Show("配置保存成功！");
            }
            finally
            {
                
                button1_Click(sender, null);//返回主窗口

            }

            

 


        }

  
  
        private void Form4_Load(object sender, EventArgs e)
        {
            //启动读取主窗体赋值
            textBox1.Text = MyDbContextOptions.DataSource;
            textBox2.Text = MyDbContextOptions.DbName;
            textBox3.Text = MyDbContextOptions.Password;
            textBox6.Text = "abc";
            textBox4.Text = MyDbContextOptions.DbName;


        }

        private void button4_Click(object sender, EventArgs e)
        {
           
            
        }
   
        public static SqlConnection mySqlConnection;  //mySqlConnection   is   a   SqlConnection   object 
  
        public static bool IsCanConnectioned = false;

        private void button3_Click(object sender, EventArgs e)
        {
            
      
            Thread.Sleep(2000);
            sqlTest();
   
        }



        public void sqlTest()
        {

            //定义数据库字符串为新输入数据
            string ConnectionString = "Data Source=" + textBox1.Text
                   + ";Initial Catalog=" + textBox4.Text
                   + ";User ID=" + textBox2.Text + ";password="
                   + textBox3.Text;
            //获取数据库连接字符串
            // ConnectionString = ConnectionInfo.ConnectionString();
            //创建连接对象

            mySqlConnection = new SqlConnection(ConnectionString);
            //ConnectionTimeout 在.net 1.x 可以设置 在.net 2.0后是只读属性，则需要在连接字符串设置
            //如：server=.;uid=sa;pwd=;database=PMIS;Integrated Security=SSPI; Connection Timeout=30
            //mySqlConnection.ConnectionTimeout = 1;//设置连接超时的时间
            try
            {
                //Open DataBase
                //打开数据库
              
                mySqlConnection.Open();
                IsCanConnectioned = true;
        
                MessageBox.Show("连接数据成功！");


            }
            catch
            {
              
                //Can not Open DataBase
                //打开不成功 则连接不成功

                IsCanConnectioned = false;
           
                MessageBox.Show("连接数据库失败，请重新输入！");
            }
            finally
            {
                //Close DataBase
                //关闭数据库连接
                mySqlConnection.Close();
    
            }
        }
    }
}